﻿using MultiLang;
namespace Rsdn.Janus
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ObjectModel;
    using Shortcuts;
    using SmartApp;
    using TreeGrid;

    public partial class OutboxDummyFormBase : UserControl, IOutboxForm, IFeatureView
    {
        private OutboxManagerBase _manager;
        private ServiceManager _serviceManager;
        private StripMenuGenerator _contextMenuGenerator;

        private delegate void Simple();

        //public abstract event EventHandler SelectedNodesChanged;
        public event EventHandler SelectedNodesChanged;

        /// <summary>
        ///   Initializes a new instance of the <see cref = "OutboxDummyForm" /> class.
        /// </summary>
        /// <param name = "provider">The provider.</param>
        /// <param name = "manager">The manager.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "provider" /> is <c>null</c>.</exception>
        public OutboxDummyFormBase(IServiceProvider provider, OutboxManagerBase manager)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");          //MLHIDE
            }

            _serviceManager = new ServiceManager(provider);
            _manager = manager;

            InitializeComponent();

            _serviceManager.Publish<IDefaultCommandService>(new DefaultCommandService("Janus.Outbox.EditItem")); //MLHIDE

            _grid.Indent = Config.Instance.ForumDisplayConfig.GridIndent;
            _grid.SmallImageList = OutboxImageManager.ImageList;
        }

        ICollection<ITreeNode> IOutboxForm.SelectedNodes
        {
            get { return _grid.SelectedNodes; }
        }

        //ICollection<ITreeNode> IOutboxForm.SelectedNodes
        //{
        //    get { return SelectedNodes; }
        //}

        /// <summary>
        ///   Clean up any resources being used.
        /// </summary>
        /// <param name = "disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_contextMenuGenerator != null)
                {
                    _contextMenuGenerator.Dispose();
                }

                if (components != null)
                {
                    components.Dispose();
                }
            }

            base.Dispose(disposing);
        }

        #region IFeatureView
        void IFeatureView.Activate(IFeature feature)
        {
            RefreshList();
        }

        public virtual void ActivateView()
        {
        }

        public virtual void DeactivateView()
        {
        }

        void IFeatureView.Refresh()
        {
        }

        void IFeatureView.ConfigChanged()
        {
        }

        public void Clear()
        {
            // no sence
        }

        #endregion IFeatureView

        public void RefreshList()
        {
            if (InvokeRequired)
            {
                Invoke((Delegate) ((Simple) RefreshList));
                return;
            }

            using (new ActiveNodeHoldHelper(_grid))
            {
                _grid.Nodes = _manager;
            }
        }
        
        protected override void OnLoad(EventArgs e)
        {
            _contextMenuGenerator = new StripMenuGenerator(_serviceManager, _contextMenuStrip, "Outbox.ContextMenu"); //MLHIDE
            base.OnLoad(e);
        }

        private void OnSelectedNodesChanged(EventArgs e)
        {
            if (SelectedNodesChanged != null)
            {
                SelectedNodesChanged(this, e);
            }
        }

        private void GridSelectedIndexChanged(object sender, EventArgs e)
        {
            OnSelectedNodesChanged(EventArgs.Empty);
        }

        private void GridDoubleClick(object sender, EventArgs e)
        {
            _serviceManager.ExecuteDefaultCommand();
        }

        private void GridMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && _grid.ActiveNode != null)
            {
                _contextMenuStrip.Show(_grid, e.Location);
            }
        }

        #region Shortcurts
        /// <summary>
        ///   Edits this instance.
        /// </summary>
        [ml_MethodShortcut(Shortcut.CtrlE, 366, "Редактировать", 367, "Редактировать сообщение.")]
        private void Edit()
        {
            _serviceManager.TryExecuteCommand("Janus.Outbox.EditItem", new Dictionary<string, object>()); //MLHIDE
        }

        /// <summary>
        ///   Deletes this instance.
        /// </summary>
        [ml_MethodShortcut((Shortcut) Keys.Delete, 368,"Удалить", 369,"Удалить сообщение.")]
        private void Delete()
        {
            _serviceManager.TryExecuteCommand("Janus.Outbox.DeleteItem", new Dictionary<string, object>()); //MLHIDE
        }
        #endregion

    }
}